<!-- src/components/Footer.vue -->
<template>
  <footer class="footer">
    <div class="container">
      <div class="footer-content">
        <div class="footer-section">
          <h3>WineShop</h3>
          <p>专业的酿酒平台</p>
          <p>为您提供优质的产品</p>
        </div>

        <div class="footer-section">
          <h4>快速链接</h4>
          <ul>
            <li><router-link to="/">首页</router-link></li>
            <li><router-link to="/search">商品搜索</router-link></li>
            <li><router-link to="/admin">管理后台</router-link></li>
          </ul>
        </div>

        <div class="footer-section">
          <h4>联系我们</h4>
          <p>📧 contact@wineshop.com</p>
          <p>📞 13879052065</p>
          <p>📍 新余市九鼎路</p>
        </div>

        <div class="footer-section">
          <h4>关注我们</h4>
          <div class="social-links">
            <span class="social-link" @click="showQr('wechat', $event)">微信</span>
            <span class="social-link" @click="showQr('douyin', $event)">抖音</span>
          </div>
        </div>
      </div>
      <!-- 二维码弹窗 -->
      <div v-if="qrVisible" class="qr-popup" :style="{ top: qrPos.top + 'px', left: qrPos.left + 'px' }" @click.self="qrVisible = false">
        <img :src="qrImage" alt="二维码" />
      </div>

      <div class="footer-bottom">
        <p>&copy; 2025 WineShop. All rights reserved.</p>
      </div>
    </div>
  </footer>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'

// 导入二维码图片
import wechatQr from '@/images/wechat-qr.jpg'
import douyinQr from '@/images/douyin-qr.jpg'

const qrVisible = ref(false)
const qrImage = ref('')
const qrPos = ref({ top: 0, left: 0 })

const qrMap = {
  wechat: wechatQr,
  douyin: douyinQr
}

// 显示二维码函数
function showQr(type, event) {
  const rect = event.target.getBoundingClientRect()

  if (qrVisible.value && qrImage.value === qrMap[type]) {
    qrVisible.value = false
    return
  }

  qrImage.value = qrMap[type]
  qrVisible.value = true
  qrPos.value = {
    top: rect.top + window.scrollY - 180,
    left: rect.left + window.scrollX + rect.width / 2 - 80
  }
}

// 失焦隐藏处理
function handleClickOutside(event) {
  const popup = document.querySelector('.qr-popup')
  const links = document.querySelectorAll('.social-link')

  const isClickInsidePopup = popup && popup.contains(event.target)
  const isClickOnLink = Array.from(links).some(link => link.contains(event.target))

  if (!isClickInsidePopup && !isClickOnLink) {
    qrVisible.value = false
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onBeforeUnmount(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.social-links {
  display: flex;
  gap: 1rem;
}

.social-link {
  cursor: pointer;
  color: #409eff;
  font-weight: bold;
  position: relative;
}

.qr-popup {
  position: absolute;
  width: 160px;
  height: 160px;
  padding: 8px;
  background: white;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
  border-radius: 8px;
  z-index: 999;
}

.qr-popup img {
  width: 150px;
  height: 150px;
  object-fit: cover; /* 保持比例缩放 */
}

.footer {
  background-color: #2c3e50;
  color: white;
  padding: 2rem 0 1rem;
  margin-top: auto;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
  margin-bottom: 2rem;
}

.footer-section h3,
.footer-section h4 {
  margin-bottom: 1rem;
  color: #ecf0f1;
}

.footer-section h3 {
  color: #f39c12;
  font-size: 1.5rem;
}

.footer-section p {
  margin-bottom: 0.5rem;
  color: #bdc3c7;
}

.footer-section ul {
  list-style: none;
  padding: 0;
}

.footer-section ul li {
  margin-bottom: 0.5rem;
}

.footer-section ul li a {
  color: #bdc3c7;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-section ul li a:hover {
  color: #f39c12;
}

.social-links {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.social-link {
  background-color: #34495e;
  padding: 0.5rem 1rem;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.social-link:hover {
  background-color: #f39c12;
}

.footer-bottom {
  border-top: 1px solid #34495e;
  padding-top: 1rem;
  text-align: center;
  color: #95a5a6;
}

@media (max-width: 768px) {
  .footer-content {
    grid-template-columns: 1fr;
    text-align: center;
  }

  .social-links {
    justify-content: center;
  }
}
</style>
